package com.dinglian.ots.controller.product;

import com.alibaba.fastjson.JSONArray;
import com.dinglian.ots.common.HttpRequest;
import com.dinglian.ots.common.ToolUtils;
import com.dinglian.ots.common.model.Page;
import com.dinglian.ots.dao.model.Sku;
import com.dinglian.ots.exception.GlobalException;
import com.dinglian.ots.result.CodeMsg;
import com.dinglian.ots.result.Result;
import com.dinglian.ots.service.ProductService;
import com.dinglian.ots.vo.product.*;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangbo
 */
@Slf4j
@RestController
@Api(value = "/product", tags = "产品模块")
@RequestMapping("/product/")
@CrossOrigin
public class ProductController {

    @Value("${bannerUrl}")
    private String bannerUrl;

    @Value("${pic.url}")
    private String url;

    @Autowired
    private ProductService productService;

    @ApiOperation("获取所有产品")
    @GetMapping("getProductList")
    public Result getProductList(@RequestParam(value = "brandId") Integer brandId,
                                 @RequestParam(value = "categoryId", required = false) Integer categoryId,
                                 @RequestParam(value = "keyword", required = false) String keyword,
                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize) {
        Page productList = new Page();
        try {
            productList = productService.getProductList(brandId, categoryId, keyword, pageNum, pageSize);
            //品牌意向通过校验
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(productList, url);
    }

    @ApiOperation("获取推荐产品")
    @GetMapping("getRecommendProduct")
    public Result getRecommendProduct(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                      @RequestParam(value = "categoryId", required = false) Integer categoryId) {
        PageInfo<ProductVo> productList = new PageInfo<>();
        try {
            productList = productService.getRecommendProduct(pageNum, pageSize, categoryId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(productList.getList(), url);
    }

    @ApiOperation("获取产品详情")
    @GetMapping("getProductInfo")
    public Result getProductInfo(@RequestParam(value = "productId") Integer productId,
                                 @RequestParam(value = "userId", required = false) Integer userId) {
        ProductInfoVo productInfoVo = new ProductInfoVo();
        try {
            productInfoVo = productService.getProductInfo(userId, productId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(productInfoVo, url);
    }

    @ApiOperation("获取产品SKU")
    @GetMapping("getProductSku")
    public Result getProductSku(@RequestParam(value = "productId") Integer productId) {

        ProductSkuVo productSkuVo = new ProductSkuVo();
        try {
            productSkuVo = productService.getProductSku(productId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(productSkuVo);
    }

    @ApiOperation("获取Banner")
    @GetMapping("getBanner")
    public Result getBanner() {
        JSONArray jsonArray = new JSONArray();
        try {
            String result = HttpRequest.sendGet(bannerUrl, "");
            result = ToolUtils.decodeUnicode(ToolUtils.decodeUnicode(result));
            jsonArray = JSONArray.parseArray(result);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(jsonArray);
    }

    @ApiOperation("品牌查询产品列表")
    @GetMapping("getProductListByBrand")
    public Result getProductListByBrand(@RequestParam(value = "brandId") Integer brandId,
                                        @RequestParam(value = "name", required = false) String name,
                                        @RequestParam(value = "stylename", required = false) String stylename,
                                        @RequestParam(value = "title", required = false) String title,
                                        @RequestParam(value = "seasonId", required = false) Integer seasonId,
                                        @RequestParam(value = "categoryId", required = false) Integer categoryId,
                                        @RequestParam(value = "subcategoryId", required = false) Integer subcategoryId,
                                        @RequestParam(value = "isShow", required = false) Integer isShow,
                                        @RequestParam(value = "status", required = false) Integer status,
                                        @RequestParam(value = "upperDate", required = false) String upperDate,
                                        @RequestParam(value = "downDate", required = false) String downDate,
                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageInfo<ProductListVo> productListVos = new PageInfo<ProductListVo>();
        try {
            productListVos = productService.getProductListByBrand(brandId, name, stylename, title, seasonId, categoryId, subcategoryId, isShow, status, upperDate, downDate, pageNum, pageSize);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successWithUrl(productListVos, url);
    }

    @ApiOperation("查询产品信息")
    @GetMapping("getProductDetail")
    public Result getProductDetail(@RequestParam("productId") Integer productId) {
        ProductDetailVo productDetailVo = new ProductDetailVo();
        try {
            productDetailVo = productService.getProductDetail(productId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(productDetailVo);
    }

    @ApiOperation("添加产品")
    @PostMapping("addProduct")
    public Result addProduct(@Validated AddProductVo productVo) {

        try {
            productService.addProduct(productVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @ApiOperation("修改产品")
    @PostMapping("editProduct")
    public Result editProduct(@Validated EditProductVo productVo) {
        try {
            productService.editProduct(productVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation("批量删除产品")
    @GetMapping("deleteProduct")
    public Result deleteProduct(String productIds) {
        try {
            productService.deleteProduct(productIds);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @ApiOperation("批量上下架产品")
    @PostMapping("upperDownProduct")
    public Result upperDownProduct(@RequestBody ShowVo showVo) {
        try {
            productService.upperDownProduct(showVo.getProductIds(), showVo.getIsShow());
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation("添加产品SKU")
    @PostMapping("addProductSku")
    public Result addProductSku(@RequestBody AddSkuVo addSkuVo) {
        SkuVo skuVo = new SkuVo();
        try {
            skuVo = productService.addProductSku(addSkuVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.success(skuVo);
    }


    @ApiOperation("修改产品SKU")
    @PostMapping("updateProductSku")
    public Result updateProductSku(@RequestBody UpdateSkuVo skuVo) {
        try {
            productService.updateProductSku(skuVo);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation("删除产品SKU")
    @PostMapping("deleteProductSku")
    public Result deleteProductSku(Integer skuId) {
        System.out.println("删除的SKUId"+skuId);
        try {
            productService.deleteProductSku(skuId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }


    @ApiOperation("批量导入产品")
    @PostMapping("batchImportProduct")
    public Result batchImportProduct(MultipartFile file, Integer brandId) {
        try {
            productService.batchImportProduct(file, brandId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

    @ApiOperation("测试批量导入产品")
    @GetMapping("test")
    public Result batchImportProduct(Integer brandId) {
        try {
            productService.test(brandId);
        } catch (GlobalException e) {
            return Result.error(e.getCm());
        } catch (Exception e) {
            return Result.error(CodeMsg.SERVER_ERROR);
        }
        return Result.successNoData();
    }

}
